{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表添加元素\n",
    "\n",
    "列表相加+ < append() < \\[列表推导式]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import timeit\n",
    "\n",
    "\n",
    "def way1(max1):\n",
    "    mylist = []\n",
    "    for i in range(max1):\n",
    "        mylist.append(i * i)\n",
    "\n",
    "\n",
    "def way2(max1):\n",
    "    mylist = [i * i for i in range(max1)]\n",
    "\n",
    "\n",
    "def way3(max1):\n",
    "    mylist = []\n",
    "    for i in range(max1):\n",
    "        mylist = mylist + [i * i]\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    t1 = timeit.Timer(\"way1(10000)\", \"from __main__ import way1\")\n",
    "    t2 = timeit.Timer(\"way2(10000)\", \"from __main__ import way2\")\n",
    "    t3 = timeit.Timer(\"way3(10000)\", \"from __main__ import way3\")\n",
    "    print(t1.timeit(number=1000))\n",
    "    print(t2.timeit(number=1000))\n",
    "    print(t3.timeit(number=1000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 搜索成员时\n",
    "\n",
    "set比list快。 列表O(n), 集合O(1)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
